/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.SABM;

import session.TX.*;
import managedBean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import entity.Outlet;
import entity.Stakeholder;
import exception.ExistException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.faces.application.FacesMessage;

/**
 *
 * @author TWD
 */
@ManagedBean(name = "outletManagerBean")
@SessionScoped
public class OutletMB {

    @EJB
    private OutletSBRemote outlet;
    
 
    private String outletName;
    private String outletAddress;
    private String phoneNumber;
    private Stakeholder stakeholder;
    private String employeeID;
    private String employeeName;
    private String password;
    private String email;
    private String statusMessage;
    private Outlet selectedOutlet;

    public OutletMB() {
    }

    public void createOutlet(ActionEvent event) {

        //Creating outlet plus outletmanager
        String outletname = String.valueOf(getOutletName());
        String phonenumber = String.valueOf(getPhoneNumber());
        String employeeid = String.valueOf(getEmployeeID());
        String employeename = String.valueOf(getEmployeeName());
        String password1 = outlet.getRandomString(10);
        String email1 = String.valueOf(getEmail());
        String outletaddress = String.valueOf(getOutletAddress());
        
        //pull the stakeholder name from the session
        String stakeholdername = (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString());
        
        try {
            getOutlet().createOutlet(stakeholdername, outletname, phonenumber, employeeid, employeename, email1, outletaddress);
            setStatusMessage(outletName + " created successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
            //Send Email to outlet manager
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com"); 
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");

            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("foodsage.tcube@gmail.com", "3332720twd");
                }
            });

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("foodsage.tcube@gmail.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(email1));
                message.setSubject("FoodSage.sg Account Confirmation");
                message.setText("Dear " + employeename + "," + "\n\n You have been registered as an Outlet Manager. \n\n Your Account information is Username:" + employeeid + " and Password:" + password1);

                Transport.send(message);

                System.out.println("Done");

            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
            System.out.print("Email Sent to Stakeholder");

        } catch (ExistException eex) {
            setStatusMessage(outletName + " already exists or Outlet Manager Id: " + employeeid + " already exists");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public List<Outlet> getAllOutlets() {
        return getOutlet().getAllOutlets();
    }

    public List<Outlet> getSpecificOutlet(ActionEvent event) {
        String o = String.valueOf(outletName);
        return getOutlet().getSpecificOutlet(o);
    }

    public void deleteOutlet(Outlet o) {
        Long outletId = o.getId();
        String outletName = o.getOutletName();
        setStatusMessage(outletName + " deleted successfully");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        outlet.deleteOutlet(outletId);
    }

    public void updateFields() {
        System.out.println("UpdateFields");
        this.outletName = selectedOutlet.getOutletName();
        this.phoneNumber = selectedOutlet.getPhoneNumber();
    }

    public String getOutletName() {
        return outletName;
    }

    public void setOutletName(String outletName) {
        this.outletName = outletName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Stakeholder getStakeholder() {
        return stakeholder;
    }

    public void setStakeholder(Stakeholder stakeholder) {
        this.stakeholder = stakeholder;
    }

    public String getEmployeeID() {
        return employeeID;
    }

    public void setEmployeeID(String employeeID) {
        this.employeeID = employeeID;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the outlet
     */
    public OutletSBRemote getOutlet() {
        return outlet;
    }

    /**
     * @param outlet the outlet to set
     */
    public void setOutlet(OutletSBRemote outlet) {
        this.outlet = outlet;
    }

    /**
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    /**
     * @return the selectedOutlet
     */
    public Outlet getSelectedOutlet() {
        return selectedOutlet;
    }

    /**
     * @param selectedOutlet the selectedOutlet to set
     */
    public void setSelectedOutlet(Outlet selectedOutlet) {
        this.selectedOutlet = selectedOutlet;
    }

    /**
     * @return the outletAddress
     */
    public String getOutletAddress() {
        return outletAddress;
    }

    /**
     * @param outletAddress the outletAddress to set
     */
    public void setOutletAddress(String outletAddress) {
        this.outletAddress = outletAddress;
    }

    
}
